
import * as echarts from 'echarts'
export const getOption = (datas) => {
    const barWidth = 20;
    const category = datas.map(data => {
        return data.name;
    });
    const values = datas.map(data => {
        return data.value;
    })
    let max = Math.max(...values);
    max = max + (max * 0.4);
    var value = [
        {
            value: 109,
            itemStyle: {
                color: '#5ee6f2'
            }
        },
        {
            value: 86,
            itemStyle: {
                color: '#2ddc9b'
            }
        }
    ];

    const bgBarData = values.map((value, index) => {
        return {
            value: max,
            itemStyle: {
                normal: {
                    borderWidth: 1,
                    borderColor: index % 2 === 0 ? '#5ee6f2' : '#2ddc9b',
                    color: 'rgba(0, 0, 0, 0)'
                }
            }
        }
    })

    return {


        grid: {
            left: 10,
            right: 30,
            bottom: 0,
            top: 20,
            // height: 280,
            containLabel: true,
        },
        xAxis: {
            // min: 0,
            // max: 2,
            type: 'category',
            axisLine: {
                show: false
            },
            splitLine: {
                show: false,
            },
            axisLabel: {
                overflow: "breakAll",
                // width: 20,
                // margin: 15,
                textStyle: {
                    fontFamily: "Microsoft YaHei",
                    color: "#fff", // x轴颜色
                    fontWeight: "normal",
                    // fontSize: "14",
                    lineHeight: 18,
                },
                // rotate: 30,
                interval: 0, //标签设置为全部显示
                formatter: (value) => {
                    let v = '';
                    value.split('').forEach((str, i) => {
                        v = v + str
                        if (i === 3) {
                            v = v + '\n'
                        }
                    })
                    return v
                }
            },
            axisTick: {
                show: false,
                interval: 0, //标签设置为全部显示
            },
            data: category
        },
        yAxis: {
            min: 0,
            max: max,
            show: false,
        },
        series: [{
            //真实值
            name: "数量",
            type: 'pictorialBar',
            // barGap: 50,
            barWidth: 15,
            symbol: 'rect',
            symbolRepeat: 'true',
            symbolMargin: '40%',
            symbolSize: ['100%', '25%'],
            symbolOffset: ['0%', -5],
            // symbolRepeat: true,
            itemStyle: {
                normal: {
                    color: (params) => {

                        return new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
                            offset: 0,
                            color: params.dataIndex % 2 === 0 ? '#1D8BE6' : '#2ddc9b',
                        },
                        {
                            offset: 1,
                            color: '#18FEFE',
                        },
                        ])
                    }
                },
                barBorderRadius: 10
            },
            data: values,
            z: 0
        }, {
            //辅助背景图形
            name: "背景条",
            type: 'bar', //pictorialBar
            barWidth: 25,
            barGap: '-100%',
            data: bgBarData,
            z: 0,
            label: {
                normal: {
                    color: '#18FDFE',
                    show: true,
                    // 调整series每个label在进度条外显示
                    position: 'top',
                    fontSize: 14,
                    formatter: function (params) {
                        return values[params.dataIndex]
                    }
                }
            },
        },]
    };
}
